/**
 * Created by joustany on 17/11/21.
 */
var page = 1
var pageCount
var pageSize = 10
$(function () {
    getShowView(page,pageSize)
})
function getShowView(page,pageSize) {
    var params = JSON.parse(window.localStorage.user)
    var redirect = JSON.parse(decodeURIComponent(params['redirect']));
    console.log(redirect.token)
    $.ajax({
        url: configApi('/order/list'),
        data: {
            access_token:redirect.token,
            page:page,
            pageSize:pageSize,
        },
        dataType: 'json',
        type: "get",
    }).done(function (res) {
        console.log(res)
        if(res.success){
            orderListView(res.data.list)
            pageCount = res.data.pages.pageCount-1 //取总页数
        }else {
            layer.open({
                content: res.message
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        }

    }).fail(function () {
        "use strict"
        layer.open({
            content: "服务器异常,请退出重试"
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    })
}
function orderListView(order) {
    "use strict"
    var html = ''
    if (order.length == 0){
        html += '<div class="order_no">暂无订单</div>'
        html += '<div class="getback" onclick="getback()">返回购物</div>'
        $('.load').hide()
    }else {
        $.each(order,function (index,item) {
            html +='<div class="order" onclick="orderHandle('+ "\'"+item.order_no +"\'"+' )">'
            $.each(item.orderItem,function (i,orders) {
                html +='<div class="me_style">'
                html +='<span>'+orders.name+'</span><span  class="order_price"><span class="order_num">x'+''+orders.num +'</span><span><span class="rmb">￥</span>'+ orders.price +'</span></span></div>'
            })
            html +='<div class="me_style"><span>'+ item.created_at+'</span><span class="order_total"><span class="font">实付:</span><span><span class="rmb">￥</span>'+item.htotal+'</span></span></div>'
            html +='</div>'
            html +='</div>'
        })
    }
    $('.orderList').append(html)
    if ($('.container .orderList').height()>$('.container').height()){
        $('.load').text('上拉加载更多')

    }else {
        $('.load').text('没有更多了')
    }
    loding = true
}
var loding = true
$(".container").scroll(function() {
    var $this     = $(this),
        viewH     = $(this).height(),//可见高度
        contentH  = $(this).get(0).scrollHeight,//内容高度
        scrollTop = $(this).scrollTop();//滚动高度
//if(contentH - viewH - scrollTop <= 100) { //到达底部100px时,加载新内容
    if (!loding){
        return
    }
    if (scrollTop / (contentH - viewH) >= 0.95) { //到达底部100px时,加载新内容
        $('.load').text('加载中...')
        if (page == pageCount){ //当前页数等于总页数
            $('.load').text('- 到底了 -')
        }else {
            page+=1
            getShowView(page,pageSize)
            loding = false

        }
    }

})
function getback() {
    window.location.href = './index.html'
}
function orderHandle(no) {
    console.log(no)
    window.location.href = './orderForm.html?order_no='+ no +''
}
